﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using School.Entities;
using School.Common;
using School.Organization;

namespace School.Pages.Organization
{
    public partial class Stages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1306) == true)
                {

                    if (!IsPostBack)
                    {
                        getdatagrid();
                        LstCampusesBind();
                        //LstCertificates.DataSource = School.Common.Structure.GetCertificatesByCampus(School.Common.Structure.GetCampuses().First().Id);
                        //LstCertificates.DataValueField = "Id";
                        //LstCertificates.DataTextField = "Name";
                        //LstCertificates.DataBind();
                        LstCertificates.Items.Insert(0, new ListItem() { Value = "0", Text = "اختيار" });
                    }
                    lblcode.Visible = false;
                    long id = 0;

                    if (long.TryParse(Request.QueryString["Id"], out id) && !IsPostBack)
                    {
                        var stage = Stage.Single(id);

                        if (stage != null)
                        {
                            LstCampuses.DataSource = School.Common.Structure.GetCampuses();
                            LstCampuses.DataValueField = "Id";
                            LstCampuses.DataTextField = "Name";
                            LstCampuses.DataBind();

                            LstCertificates.DataSource = School.Common.Structure.GetCertificatesByCampus(stage.ORG_Certificate.ORG_Campus.Id);
                            LstCertificates.DataValueField = "Id";
                            LstCertificates.DataTextField = "Name";
                            LstCertificates.DataBind();

                            LstCampuses.SelectedValue = stage.ORG_Certificate.ORG_Campus.Id.ToString();
                            LstCertificates.SelectedValue = stage.ORG_Certificate.Id.ToString();
                            TxtName.Text = stage.Name;
                            TxtDescription.Text = stage.Description;
                            TxtCode.Text = stage.Code;
                        }
                        else
                        {
                            Response.Redirect("Stages.aspx");
                        }
                    }

                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");

            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        private void LstCampusesBind()
        {
            LstCampuses.DataSource = School.Common.Structure.GetCampuses();
            //LstCampuses.DataValueField = "Id";
            //LstCampuses.DataTextField = "Name";
            LstCampuses.Items.Insert(0, new ListItem("اختيار", "-1"));
            LstCampuses.DataBind();
        }

        protected void getdatagrid()
        {
            GridView1.DataSource = School.Common.Structure.GetStages().Select(x => new { Name = x.Name, Id = x.Id, Description = x.Description, CName = x.ORG_Certificate.ORG_Campus.Name, CeName = x.ORG_Certificate.Name, x.Code });
            GridView1.DataBind();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            long id = 0;

            if (long.TryParse(Request.QueryString["Id"], out id))
                Stage.Update(new ORG_Stage() { Id = id, Name = TxtName.Text, Description = TxtDescription.Text, Code = TxtCode.Text, Certificate_Id = long.Parse(LstCertificates.SelectedItem.Value) });
            else
                Stage.Add(new ORG_Stage() { Name = TxtName.Text, Description = TxtDescription.Text, Code = TxtCode.Text, Certificate_Id = long.Parse(LstCertificates.SelectedItem.Value) });

            Response.Redirect("Stages.aspx");
            getdatagrid();

        }

        protected void LstCampuses_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstCampuses.SelectedIndex > 0)
            {
                LstCertificates.DataSource = School.Common.Structure.GetCertificatesByCampus(long.Parse(LstCampuses.SelectedValue));
                ddlBind(LstCertificates);
                //LstCertificates.DataValueField = "Id";
                //LstCertificates.DataTextField = "Name";
                //LstCertificates.DataBind();
            }
        }

        protected void LstCertificates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstCertificates.SelectedIndex > 0)
            {
                lblcode.Text = "الكود=" + School.Common.Structure.get_Certi_Code(Convert.ToInt32(LstCertificates.SelectedValue));
                lblcode.Visible = true;
            }

        }
    }
}